package jnBase.script
{
	import jnBase.Api;
	import jnBase.Data;
	import jnBase.InsSceneTools;
	import jnBase.ShiyanControl;
	import jnBase.eventdispatcher.JNEventDispatcher;
	import jnBase.view.SceneToolControl;
	import jnBase.view.SceneToolsWAP;
	
	import laya.components.Script;
	import laya.display.Scene;
	import laya.media.SoundManager;
	import laya.ui.Image;
	import laya.utils.Browser;
	
	public class ScriptSceneTools extends Script
	{
		private var _owner:Scene;
		private var ms:Image;
		public function ScriptSceneTools()
		{
			super();
		}
		override public function onAwake():void
		{
			// TODO Auto Generated method stub
			_owner=owner as Scene;
			SoundManager.autoStopMusic=false;
			SoundManager.musicVolume=0.2;
			SoundManager.playMusic(Data.bgMusic);
			ShiYanBaoGao.getInstance().visible=false;
			ShiYanBaoGao.getInstance().popup();
			//实验报告数据初始化完成
			ShiYanBaoGao.getInstance().once("getData",this,function():void{
				ShiYanBaoGao.getInstance().close();
				Laya.timer.once(300,this,function():void{
					ShiYanBaoGao.getInstance().visible=true;
				});
			});
			JNEventDispatcher.getInstance().on(JNEventDispatcher.ClOSE_TOOLS,_owner,(_owner as InsSceneTools).closeToolsHandler);
			JNEventDispatcher.getInstance().on(JNEventDispatcher.CHOOSE_TOOLS,_owner,(_owner as InsSceneTools).chooseToolsHandler);
			JNEventDispatcher.getInstance().on(JNEventDispatcher.INeedTools,_owner,(_owner as InsSceneTools).getToolByValue);
			JNEventDispatcher.getInstance().on(JNEventDispatcher.SHOW_STEP,_owner,(_owner as InsSceneTools).showStepByIndex);
			JNEventDispatcher.getInstance().on(JNEventDispatcher.SHOW_SON_STEP,_owner,(_owner as InsSceneTools).showSonStepByIndex);
			JNEventDispatcher.getInstance().on(JNEventDispatcher.SHOW_TIPS,this,showTips);
			JNEventDispatcher.getInstance().on(JNEventDispatcher.HIDE_TIPS,_owner,(_owner as InsSceneTools).hideTips);
			
			examTime=Data.examTime;
		}
		private function showTips(str:String,bAutoHide:Boolean=false,interval:int=5000):void{
			//如果是空字符串，则不提示
			if(str.length<1){
				return ;
			}
			(_owner as InsSceneTools).showTips(str,bAutoHide,interval);
		}
		/**
		 * 
		 * 从服务器获取需要继续操作的页面索引
		 * 初始化数据
		 */		
		public function initData():void
		{
			getStepData();
			if(Data.token){
				Data.EndTime=new Date().toString();
				Api.GetTokenUser(this,function(res){
					(_owner as InsSceneTools).getUserInfo(res)
				});
			}
		}
		/**
		 *获取到服务器存储时间 
		 */		
		private var getSeverTime:Boolean=false;
		/**
		 *获取服务器上保存的步骤信息 
		 * 
		 */		
		private function getStepData():void{
			if(Data.token){
				//获取服务器记录的练习位置。
				if(Data.practiceInfo.step=="0"){
					showDefault();
				}
				else{
					(_owner as InsSceneTools).showStepByValue(Data.practiceInfo.step);
				}
				if(Data.practiceInfo){
					getSeverTime=true;
					Data.UsedTime=Data.practiceInfo.usedtime;
				}
			}
			else{
				setDefaultTime();
				showDefault();
			}
			/**
			 *初始化时间0； 
			 * 
			 */		
			function setDefaultTime():void
			{
				// TODO Auto Generated method stub
				getSeverTime=true;
				Data.UsedTime=0;
			}
		}
		private function showDefault():void{
			if(ShiyanControl.hasSonStep(0)){
				(_owner as InsSceneTools).showStepByValue("1*1");
				trace("重新设定当前步骤：1*1");
			}
			else{
				(_owner as InsSceneTools).showStepByValue("1*0");
				trace("重新设定当前步骤：1*0");
			}
		}
		public var createBoxInterval:Number = 1000;
		private var _time:Number=0;
		override public function onUpdate():void
		{
			// TODO Auto Generated method stub
			super.onUpdate();
			(_owner as InsSceneTools).update();
			if(!getSeverTime) return;
			var now:* = Browser.now();
			if (now - this._time > this.createBoxInterval) {
				this._time = now;
				updateTime();
			}
		}
		private var examTime:int;
		/**
		 *更新时间 
		 * 
		 */		
		private function updateTime():void
		{
			// TODO Auto Generated method stub
//			trace("总用时："+Data.UsedTime+";本次用时："+examTime);
			//考核模式：
			if(Data.timeLimit){
				if(Data.isExamComplete){
					return endExam(true)
				}
				else if(Data.UsedTime>Data.examTime){
					return endExam(false)
				}
			}
			//每秒上传实验用时。
			if(Data.timeLimit) examTime=Data.examTime-Data.UsedTime;
			(_owner as InsSceneTools).updateTime(formatTime(Data.UsedTime),formatTime(examTime));
			Data.UsedTime++;
			Data.perStepTime++;
		}
		/**
		 *考核模式：考核结束 
		 * 
		 */		
		private function endExam($boo:Boolean):void
		{
			// TODO Auto Generated method stub
			trace("考核时间到");
			this.enabled=false;//不再计时
			//超时
			if(!$boo) {
				JNEventDispatcher.getInstance().event("TimeOut");
				if(Browser.onPC){
					SceneToolControl.getInstance().pauseMusic();
				}
				else if(Browser.onMobile){
					SceneToolsWAP.getInstance().pauseMusic();
				}
			}
			SoundManager.stopAllSound();
		}
		/**
		 *转换时间格式 
		 * @param param0
		 * @return 
		 * 
		 */		
		public static function formatTime(param0:Number):String
		{
			// TODO Auto Generated method stub
			var h:String=format2(Math.floor(param0/3600));
			var m:String=format2(Math.floor((param0-parseInt(h)*3600)/60));
			var s:String=format2(param0%60);
			return h+":"+m+":"+s;
		}
		/**
		 *将数字变成2位字符串 
		 * @param param0
		 * @return 
		 * 
		 */		
		public static  function format2(param0:Number):String
		{
			// TODO Auto Generated method stub
			if(param0<10){
				return "0"+param0;
			}
			else return param0+"";
		}
		
	}
}